package com.cms.controller.admin;

import com.cms.domain.Feedback;
import com.cms.utils.BackupUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * Controller - 数据库
 * 
 * 
 * 
 */
@Controller("adminDatabaseController")
@RequestMapping("/admin/database")
public class DatabaseController extends BaseController {
	
    /**
     * 列表
     */
    @RequestMapping
    public String index(ModelMap modelMap) {
        List<String> backups = BackupUtils.getFiles();
        Collections.reverse(backups);
        modelMap.addAttribute("backups", backups);
        return getView("database/index");
    }
    
    /**
     * 备份
     */
    @ResponseBody
    @RequestMapping("/backup")
    public Feedback backup(){
        BackupUtils.backup();
        return Feedback.success(new HashMap<>());
    }
    
    /**
     * 还原
     */
    @ResponseBody
    @RequestMapping("/restore")
    public Feedback restore(String name){
        BackupUtils.restore(name);
        return Feedback.success(new HashMap<>());
    }

    
    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Feedback delete(String name) {
        BackupUtils.delete(name);
        return Feedback.success(new HashMap<>());
    }
}
